package com.yuncheng.spcyApi.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.stream.Collectors;
import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.spcyApi.utils.DateConvert;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.vo.HttpResult;

import com.yuncheng.common.base.controller.BaseController;
import com.yuncheng.spcyApi.entity.SpcyQyWjml;
import com.yuncheng.spcyApi.service.ISpcyQyWjmlService;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiImplicitParam;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import utils.LogUtils;
import utils.ObjectToMapUtils;

/**
 * @Description: 企业-文件目录
 * @Author: yuncheng
 * @Date: 2023-07-08
 * @Version: V1.0
 */
@Api(tags = "企业-文件目录接口")
@RestController
@RequestMapping("/api/spzx-spcy/spcyQyWjml")
public class SpcyQyWjmlController extends BaseController<SpcyQyWjml, ISpcyQyWjmlService> {

    private static final Logger log = LoggerFactory.getLogger(SpcyQyWjmlController.class);

    @Resource
    private ISpcyQyWjmlService spcyQyWjmlService;

    @Resource
    private ISpcyLogService spcyLogService;


    @Log(title = "企业-文件目录-分页列表查询")
    @ApiOperation(value = "企业-文件目录-分页列表查询")
    @GetMapping(value = "/queryWjmlPageList")
    public HttpResult<?> queryWjmlPageList(SpcyQyWjml spcyQyWjml,
                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SpcyQyWjml> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<SpcyQyWjml> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(spcyQyWjml.getFwjlx1()),SpcyQyWjml::getFwjlx1,spcyQyWjml.getFwjlx1());
        queryWrapper.like(StringUtils.isNotEmpty(spcyQyWjml.getFwjlx2()),SpcyQyWjml::getFwjlx2,spcyQyWjml.getFwjlx2());
        queryWrapper.like(StringUtils.isNotEmpty(spcyQyWjml.getFwjlx3()),SpcyQyWjml::getFwjlx3,spcyQyWjml.getFwjlx3());
        queryWrapper.like(StringUtils.isNotEmpty(spcyQyWjml.getFlxmc()),SpcyQyWjml::getFlxmc,spcyQyWjml.getFlxmc());
        queryWrapper.orderByAsc(SpcyQyWjml::getFwjlx1);
        queryWrapper.orderByAsc(SpcyQyWjml::getForder);
        queryWrapper.orderByAsc(SpcyQyWjml::getCreateTime);
        IPage<SpcyQyWjml> pageList = spcyQyWjmlService.page(page, queryWrapper);
        return HttpResult.ok(pageList);
    }

    @Log(title = "获取文件目录类型")
    @ApiOperation(value = "获取文件目录类型")
    @ApiImplicitParam(name = "ftype1",value = "目录类型1",required = true)
    @GetMapping(value = "queryWjmlTypeList")
    public HttpResult<?> queryWjmlTypeList(@RequestParam("ftype1") String ftype1){
        if (StringUtils.isBlank(ftype1)){
            return HttpResult.error("目录类型不能为空");
        }
        LambdaQueryWrapper<SpcyQyWjml> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcyQyWjml::getFwjlx1,ftype1);
        queryWrapper.orderByAsc(SpcyQyWjml::getForder);
        queryWrapper.orderByAsc(SpcyQyWjml::getCreateTime);
        List<SpcyQyWjml> wjmlList = spcyQyWjmlService.list(queryWrapper);
        List<String> wjlxList = new ArrayList<>();

        if (CollectionUtils.isNotEmpty(wjmlList)) {
            wjlxList = wjmlList.stream().map(SpcyQyWjml::getFlxmc).filter(f->StringUtils.isNotBlank(f)).distinct().collect(Collectors.toList());
        }
        Map resultMap = new HashMap();
        resultMap.put("wjlxList", wjlxList);

        return HttpResult.ok(resultMap);
    }

    @Log(title = "企业-文件目录-添加")
    @ApiOperation(value = "企业-文件目录-添加")
    @PostMapping(value = "/addWjml")
    public HttpResult<?> addWjml(@RequestBody SpcyQyWjml spcyQyWjml) {
        String operContent = "新增信息：";

        spcyQyWjml.setId(GetUuIdUtils.ReplaceUuId());
        spcyQyWjmlService.save(spcyQyWjml);
        // 将map里面的属性和值转String
        Map<String, Object> beanMap = ObjectToMapUtils.objectToMap(spcyQyWjml);
        operContent += LogUtils.MapToStringMethod(beanMap);
        spcyLogService.addLogs(LogConstant.LOGS_QY_WJML,operContent,spcyQyWjml.getId(), LogConstant.LOGS_QY_WJML, SpcyConstant.LOGS_SYS_TYPE_XZ);
        return HttpResult.ok("添加成功");
    }

    @Log(title = "企业-文件目录-编辑")
    @ApiOperation(value = "企业-文件目录-编辑")
    @PutMapping(value = "/editWjml")
    public HttpResult<?> editWjml(@RequestBody HashMap<String, String> buffer)throws InvocationTargetException, IllegalAccessException  {
        if (StringUtils.isEmpty(buffer.get("id"))){
            return HttpResult.error("文件目录信息id不能为空");
        }
        String wjmlId = buffer.get("id");
        SpcyQyWjml oldWjml = spcyQyWjmlService.getById(wjmlId);
        SpcyQyWjml oldWjmlUpdate = spcyQyWjmlService.getById(wjmlId);
        if (oldWjmlUpdate == null){
            return HttpResult.error("当前文件目录基本信息数据不存在，请重试");
        }
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(oldWjmlUpdate, buffer);
        spcyQyWjmlService.updateById(oldWjmlUpdate);

        String operContent = "基本信息修改：";
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(oldWjml);
        operContent += LogUtils.BeanModifyMsg(oldWjmlUpdate,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_QY_WJML,operContent,wjmlId,LogConstant.LOGS_QY_WJML,SpcyConstant.LOGS_SYS_TYPE_XG);
        return HttpResult.ok("修改成功");
    }

    @Log(title = "企业-文件目录-批量删除")
    @ApiOperation(value = "企业-文件目录-批量删除")
    @DeleteMapping(value = "/deleteBatchWjml")
    public HttpResult<?> deleteBatchWjml(@RequestParam("ids") String ids) {
        if (StringUtils.isEmpty(ids)){
            return HttpResult.error("ids参数不能为空");
        }
        List<String> idsList = Arrays.asList(ids.split(","));
        List<SpcyQyWjml> sqsxWjmlList = spcyQyWjmlService.listByIds(idsList);
        if (CollectionUtils.isEmpty(sqsxWjmlList)){
            return HttpResult.error("数据不存在，请重试");
        }
        String operContent = "";
        for (SpcyQyWjml wjml : sqsxWjmlList){
            String id = wjml.getId();
            spcyQyWjmlService.removeById(id);

            operContent = "删除信息：";
            Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(wjml);
            operContent += LogUtils.MapToStringMethod(oldMap);
            spcyLogService.addLogs(LogConstant.LOGS_QY_WJML,operContent,id,LogConstant.LOGS_QY_WJML,SpcyConstant.LOGS_SYS_TYPE_SC);
            operContent = "";
        }

        return HttpResult.ok("删除成功");
    }

    @Log(title = "企业-文件目录-通过id查询")
    @ApiOperation(value = "企业-文件目录-通过id查询")
    @GetMapping(value = "/queryById")
    public HttpResult<?> queryById(@RequestParam("id") String id) {
        SpcyQyWjml spcyQyWjml = spcyQyWjmlService.getById(id);
        return HttpResult.ok(spcyQyWjml);
    }

}
